package com.hx.controller;

import java.util.Objects;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class Controller01 {



    @RequestMapping("/test1")
    public String test1() {
        System.out.println("test1");
        return "success";
    }

    /**
     * 路径传参
     * @param id
     * @param username
     * @return
     */
    @RequestMapping("/test2/{id}/{username}")
    public String test2(@PathVariable(name = "id") Integer id,@PathVariable(name = "username") String username) {
        System.out.println(id + username);
        return "success";
    }

    /**
     * 获取原生servlet
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping("/test3")
    public String test3(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println("request="+request);
        System.out.println("response="+ response);
        System.out.println( "session=" +session);
        return "success";
    }

    /**
     * CookieValue 用来获取cookie
     * @param request
     * @param token
     * @return
     */
    @RequestMapping("/test4")
    public String test4(HttpServletRequest request, @CookieValue(name="token",required = false,defaultValue = "test_token")String token) {
        Cookie[] cookies = request.getCookies();
        if (!Objects.isNull(cookies)){
            for (Cookie cookie : cookies) {
                if("token".equals(cookie.getName())){
                    System.out.println(cookie.getName()+":"+cookie.getValue());
                }
            }
        }
        System.out.println("token="+token);
        return "success";
    }
}
